package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dao.BaseDao;
import com.dao.ZhengZhuangDao;
import com.entity.ZhengHou;
import com.entity.ZhengZhuang;

public class ZhengZhuangDaoImpl extends BaseDao implements ZhengZhuangDao {
	private Connection con = null;
	private PreparedStatement ps = null;
	
	@Override
	public List<ZhengZhuang> searchZz(String searchZZType,String searchZZValue) {
		// TODO Auto-generated method stub
		ResultSet rs = null;
		List<ZhengZhuang> zzlist=new ArrayList<ZhengZhuang>();
		String sql=null;
		ZhengZhuang zhengzhuang=null;
		if(searchZZType.equals("1")){
			sql="select * from zhengzhuang where zzname='"+searchZZValue+"'";
		}
		if(searchZZType.equals("2")){
			Integer id=new Integer(searchZZValue);
			sql="select * from zhengzhuang where zzid="+id+"";
		}
		if(searchZZType.equals("3")){
			sql="select * from zhengzhuang where zzname like '%"+searchZZValue+"%' ";
		}
		if(searchZZType.equals("4")){
			sql="select zz.* from zhengzhuang zz,zhengzhuangalias zza where zza.zzid=zz.zzid and zza.zzalias='"+searchZZValue+"'";
		}
		try{
			con=this.getConn();
			ps = con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				zhengzhuang=new ZhengZhuang();
				zhengzhuang.setZzId(rs.getInt("zzid"));
				zhengzhuang.setZzNumber(rs.getInt("zznumber"));
				zhengzhuang.setZzName(rs.getString("zzname"));
				zhengzhuang.setZhList(this.searchZh(zhengzhuang.getZzId()));
				zzlist.add(zhengzhuang);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			this.closeAll(con, ps, rs);
		}
		return zzlist;
	}
	@Override
	public List<ZhengHou> searchZh(int zzid) {
		// TODO Auto-generated method stub
	    ResultSet rs = null;
		ZhengHou zhenghou;
		List<ZhengHou> zhlist=new ArrayList<ZhengHou>();
		try{
			String sql="select * from zhenghou where zzid="+zzid+"";
			con=this.getConn();
			ps = con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				zhenghou=new ZhengHou();
				zhenghou.setZhId(rs.getInt("zhid"));
				zhenghou.setZhName(rs.getString("zhname"));
				zhenghou.setZhDescript(rs.getString("zhdescript"));
				zhlist.add(zhenghou);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return zhlist;
	}
	

}
